/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "UnpackThemeTask.h"
#include "../../Controls/Application.h"
#include "../../Prompts/ProgressWindow.h"
#include "../../FileOperations/fileops.h"

UnpackThemeTask::UnpackThemeTask(const std::string &title, const std::string &dest, ZipFile *z)
	: zipfile(z), themeTitle(title), destPath(dest)
{
	running = true;
	CopySize = 0;
	Filename = "";
	
	ThreadedTaskHandler::Instance()->AddTask(this);
}

UnpackThemeTask::~UnpackThemeTask()
{
	if(zipfile)
		delete zipfile;
}

u64 UnpackThemeTask::GetUnpackSize(void)
{
	u64 extractSize = 0;

	for(u32 i = 0; i < zipfile->GetItemCount(); ++i)
	{
		ArchiveFileStruct *item = zipfile->GetFileStruct(i);
		if(!item)
			continue;
		
		if(!item->isdir)
			extractSize += item->length;
	}

	return extractSize;
}

void UnpackThemeTask::Execute(void)
{
	if(!zipfile)
	{
		running = false;
		return;
	}

	if(ProgressWindow::Instance()->IsRunning())
		ProgressWindow::Instance()->SetTitle(tr("Calculating extract size..."));
	else
		StartProgress(tr("Calculating extract size..."));

	CopySize = GetUnpackSize();

	ProgressWindow::Instance()->SetTitle(tr("Extracting File(s):"));
	ProgressWindow::Instance()->SetCompleteValues(0, CopySize);

	std::string destPathSlash = (destPath.size() > 0 && destPath[destPath.size()-1] != '/') ? destPath + '/' : destPath;

	int result = zipfile->ExtractAll(destPathSlash.c_str());
	
	if((result < 0) && !Application::isClosing() && result != PROGRESS_CANCELED)
	{
		ThrowMsg(tr("Error:"), tr("Failed extracting some item(s)."));
	}
	
	std::string themeDir;
	zipfile->FindFilePart(".them", Filename);
	zipfile->FindFilePart("/", themeDir);

	if(!zipfile->FindFile("theme_preview.png") && themeDir.size() != 0)
	{
		size_t pos = themeDir.find("/");
		if(pos != std::string::npos)
		{
			themeDir.erase(pos);
			char imgfilepath[255];
			char newfilepath[255];
			snprintf(imgfilepath, sizeof(imgfilepath), "%s/tmp/%s.jpg", Settings.ThemePath, themeTitle.c_str());
			snprintf(newfilepath, sizeof(newfilepath), "%s/%s/theme_preview.png", Settings.ThemePath, themeDir.c_str());
			CopyFile(imgfilepath, newfilepath);
		}
	}
	
	RemoveFile(zipfile->GetFullPath());
	
	StopProgress();
	while(ProgressWindow::Instance()->IsRunning())
		usleep(50);

	running = false;
}
